home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Languguage OS 2
/
Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO
/
gnu
/
elispman.lha
/
elispman
/
elisp-14
(
.txt
)
< prev
next >
Wrap
GNU Info File
|
1993-06-01
|
51KB
|
938 lines
This is Info file elisp, produced by Makeinfo-1.55 from the input file
elisp.texi.
This is edition 2.0 of the GNU Emacs Lisp Reference Manual, for
Emacs Version 19.
Published by the Free Software Foundation, 675 Massachusetts Avenue,
Cambridge, MA 02139 USA
Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided that
the entire resulting derived work is distributed under the terms of a
permission notice identical to this one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that this permission notice may be stated in a
translation approved by the Foundation.
File: elisp, Node: Classifying Events, Next: Accessing Events, Prev: Event Examples, Up: Input Events
Classifying Events
------------------
Every event has an "event type" which classifies the event for key
binding purposes. For a keyboard event, the event type equals the event
value; thus, the event type for a character is the character, and the
event type for a function key symbol is the symbol itself. For events
which are lists, the event type is the symbol in the CAR of the list.
Thus, the event type is always a symbol or a character.
Two events of the same type are equivalent where key bindings are
concerned; thus, they always run the same command. That does not
necessarily mean they do the same things, however, as some commands look
at the whole event to decide what to do. For example, some commands use
the location of a mouse event to decide what text to act on.
Sometimes broader classifications of events are useful. For example,
you might want to ask whether an event involved the META key,
regardless of which other key or mouse button was used.
To get such information conveniently, call the functions
`event-modifiers' and `event-basic-type'.
- Function: event-modifiers EVENT
This function returns a list of the modifiers that EVENT has. The
modifiers are symbols; they include `shift', `control', `meta',
`alt', `hyper' and `super'. In addition, the property of a mouse
event symbol always has one of `click', `drag', and `down' among
the modifiers. For example:
(event-modifiers ?a)
=> nil
(event-modifiers ?\C-a)
=> (control)
(event-modifiers ?\C-%)
=> (control)
(event-modifiers ?\C-\S-a)
=> (control shift)
(event-modifiers 'f5)
=> nil
(event-modifiers 's-f5)
=> (super)
(event-modifiers 'M-S-f5)
=> (meta shift)
(event-modifiers 'mouse-1)
=> (click)
(event-modifiers 'down-mouse-1)
=> (down)
The modifiers list for a click event explicitly contains `click',
but the event symbol name itself does not contain `click'.
- Function: event-basic-type EVENT
This function returns the key or mouse button that EVENT
describes, with all modifiers removed. For example:
(event-basic-type ?a)
=> 97
(event-basic-type ?A)
=> 97
(event-basic-type ?\C-a)
=> 97
(event-basic-type ?\C-\S-a)
=> 97
(event-basic-type 'f5)
=> f5
(event-basic-type 's-f5)
=> f5
(event-basic-type 'M-S-f5)
=> f5
(event-basic-type 'down-mouse-1)
=> mouse-1
- Function: mouse-movement-p OBJECT
This function returns non-`nil' if OBJECT is a mouse movement
event.
File: elisp, Node: Accessing Events, Next: Strings of Events, Prev: Classifying Events, Up: Input Events
Accessing Events
----------------
This section describes convenient functions for accessing the data in
an event which is a list.
The following functions return the starting or ending position of a
mouse-button event. The position is a list of this form:
(WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
- Function: event-start EVENT
This returns the starting position of EVENT.
If EVENT is a click or button-down event, this returns the
location of the event. If EVENT is a drag event, this returns the
drag's starting position.
- Function: event-end EVENT
This returns the ending position of EVENT.
If EVENT is a drag event, this returns the position where the user
released the mouse button. If EVENT is a click or button-down
event, the value is actually the starting position, which is the
only position such events have.
These four functions take a position-list as described above, and
return various parts of it.
- Function: posn-window POSITION
Return the window that POSITION is in.
- Function: posn-point POSITION
Return the buffer location in POSITION.
- Function: posn-col-row POSITION
Return the row and column in POSITION, as a list `(COL . ROW)'.
- Function: posn-timestamp POSITION
Return the timestamp of POSITION.
- Function: scroll-bar-scale RATIO TOTAL
This function multiples (in effect) RATIO by TOTAL, rounding the
result to an integer. RATIO is not a number, but rather a pair
`(NUM . DENOM)'.
This is handy for scaling a position on a scroll bar into a buffer
position. Here's how to do that:
(scroll-bar-scale (posn-col-row (event-start event))
(buffer-size))
File: elisp, Node: Strings of Events, Prev: Accessing Events, Up: Input Events
Putting Keyboard Events in Strings
----------------------------------
In most of the places where strings are used, we conceptualize the
string as containing text characters--the same kind of characters found
in buffers or files. Occasionally Lisp programs use strings which
conceptually contain keyboard characters; for example, they may be key
sequences or keyboard macro definitions. There are special rules for
how to put keyboard characters into a string, because they are not
limited to the range of 0 to 255 as text characters are.
A keyboard character typed using the META key is called a "meta
character". The numeric code for such an event includes the 2**23 bit;
it does not even come close to fitting in a string. However, earlier
Emacs versions used a different representation for these characters,
which gave them codes in the range of 128 to 255. That did fit in a
string, and many Lisp programs contain string constants that use `\M-'
to express meta characters, especially as the argument to `define-key'
and similar functions.
We provide backward compatibility to run those programs with special
rules for how to put a keyboard character event in a string. Here are
the rules:
* If the keyboard event value is in the range of 0 to 127, it can go
in the string unchanged.
* The meta variants of those events, with codes in the range of
2**23 to 2**23+127, can also go in the string, but you must change
their numeric values. You must set the 2**7 bit instead of the
2**23 bit, resulting in a value between 128 and 255.
* Other keyboard character events cannot fit in a string. This
includes keyboard events in the range of 128 to 255.
Functions such as `read-key-sequence' that can construct strings
containing events follow these rules.
When you use the read syntax `\M-' in a string, it produces a code
in the range of 128 to 255--the same code that you get if you modify
the corresponding keyboard event to put it in the string. Thus, meta
events in strings work consistently regardless of how they get into the
strings.
New programs can avoid dealing with these rules by using vectors
instead of strings for key sequences when there is any possibility that
these issues might arise.
The reason we changed the representation of meta characters as
keyboard events is to make room for basic character codes beyond 127,
and support meta variants of such larger character codes.
File: elisp, Node: Reading Input, Next: Waiting, Prev: Input Events, Up: Command Loop
Reading Input
=============
The editor command loop reads keyboard input using the function
`read-key-sequence', which uses `read-event'. These and other
functions for keyboard input are also available for use in Lisp
programs. See also `momentary-string-display' in *Note Temporary
Displays::, and `sit-for' in *Note Waiting::. *Note Terminal Input::,
for functions and variables for controlling terminal input modes and
debugging terminal input.
For higher-level input facilities, see *Note Minibuffers::.
* Menu:
* Key Sequence Input:: How to read one key sequence.
* Reading One Event:: How to read just one event.
* Quoted Character Input:: Asking the user to specify a character.
* Peeking and Discarding:: How to reread or throw away input events.
File: elisp, Node: Key Sequence Input, Next: Reading One Event, Up: Reading Input
Key Sequence Input
------------------
The command loop reads input a key sequence at a time, by calling
`read-key-sequence'. Lisp programs can also call this function; for
example, `describe-key' uses it to read the key to describe.
- Function: read-key-sequence PROMPT
This function reads a key sequence and returns it as a string or
vector. It keeps reading events until it has accumulated a full
key sequence; that is, enough to specify a non-prefix command
using the currently active keymaps.
If the events are all characters and all can fit in a string, then
`read-key-sequence' returns a string (*note Strings of Events::.).
Otherwise, it returns a vector, since a vector can hold all kinds
of events--characters, symbols, and lists. The elements of the
string or vector are the events in the key sequence.
Quitting is suppressed inside `read-key-sequence'. In other words,
a `C-g' typed while reading with this function is treated like any
other character, and does not set `quit-flag'. *Note Quitting::.
The argument PROMPT is either a string to be displayed in the echo
area as a prompt, or `nil', meaning not to display a prompt.
In the example below, the prompt `?' is displayed in the echo area,
and the user types `C-x C-f'.
(read-key-sequence "?")
---------- Echo Area ----------
?`C-x C-f'
---------- Echo Area ----------
=> "^X^F"
- Variable: num-input-keys
This variable's value is the number of key sequences processed so
far in this Emacs session. This includes key sequences read from
the terminal and key sequences read from keyboard macros being
executed.
If an input character is an upper case letter and has no key binding,
but the lower case equivalent has one, then `read-key-sequence'
converts the character to lower case. Note that `lookup-key' does not
perform case conversion in this way.
The function `read-key-sequence' also transforms some mouse events.
It converts unbound drag events into click events, and discards unbound
button-down events entirely. It also reshuffles focus events so that
they never appear in a key sequence with any other events.
When mouse events occur in special parts of a window, such as a mode
line or a scroll bar, the event itself shows nothing special--only the
symbol that would normally represent that mouse button and modifier
keys. The information about the screen region is kept elsewhere in the
event--in the coordinates. But `read-key-sequence' translates this
information into imaginary prefix keys, all of which are symbols:
`mode-line', `vertical-line', `horizontal-scroll-bar' and
`vertical-scroll-bar'.
For example, if you call `read-key-sequence' and then click the
mouse on the window's mode line, this is what happens:
(read-key-sequence "Click on the mode line: ")
=> [mode-line
(mouse-1
(#<window 6 on NEWS> mode-line
(40 . 63) 5959987))]
You can define meanings for mouse clicks in special window regions by
defining key sequences using these imaginary prefix keys.
File: elisp, Node: Reading One Event, Next: Quoted Character Input, Prev: Key Sequence Input, Up: Reading Input
Reading One Event
-----------------
The lowest level functions for command input are those which read a
single event.
- Function: read-event
This function reads and returns the next event of command input,
waiting if necessary until an event is available. Events can come
directly from the user or from a keyboard macro.
The function `read-event' does not display any message to indicate
it is waiting for input; use `message' first, if you wish to
display one. If you have not displayed a message, `read-event'
does "prompting": it displays descriptions of the events that led
to or were read by the current command. *Note The Echo Area::.
If `cursor-in-echo-area' is non-`nil', then `read-event' moves the
cursor temporarily to the echo area, to the end of any message
displayed there. Otherwise `read-event' does not move the cursor.
Here is what happens if you call `read-event' and then press the
right-arrow function key:
(read-event)
=> right
- Function: read-char
This function reads and returns a character of command input. It
discards any events that are not characters until it gets a
character.
In the first example, the user types `1' (which is ASCII code 49).
The second example shows a keyboard macro definition that calls
`read-char' from the minibuffer. `read-char' reads the keyboard
macro's very next character, which is `1'. The value of this
function is displayed in the echo area by the command
`eval-expression'.
(read-char)
=> 49
(symbol-function 'foo)
=> "^[^[(read-char)^M1"
(execute-kbd-macro foo)
-| 49
=> nil
File: elisp, Node: Quoted Character Input, Next: Peeking and Discarding, Prev: Reading One Event, Up: Reading Input
Quoted Character Input
----------------------
You can use the function `read-quoted-char' when you want the user
to specify a character, and allow the user to specify a control or meta
character conveniently with quoting or as an octal character code. The
command `quoted-insert' calls this function.
- Function: read-quoted-char &optional PROMPT
This function is like `read-char', except that if the first
character read is an octal digit (0-7), it reads up to two more
octal digits (but stopping if a non-octal digit is found) and
returns the character represented by those digits as an octal
number.
Quitting is suppressed when the first character is read, so that
the user can enter a `C-g'. *Note Quitting::.
If PROMPT is supplied, it specifies a string for prompting the
user. The prompt string is always printed in the echo area and
followed by a single `-'.
In the following example, the user types in the octal number 177
(which is 127 in decimal).
(read-quoted-char "What character")
---------- Echo Area ----------
What character-`177'
---------- Echo Area ----------
=> 127
File: elisp, Node: Peeking and Discarding, Prev: Quoted Character Input, Up: Reading Input
Peeking and Discarding
----------------------
- Variable: unread-command-events
This variable holds a list of events waiting to be read as command
input. The events are used in the order they appear in the list.
The variable is used because in some cases a function reads a
event and then decides not to use it. Storing the event in this
variable causes it to be processed normally by the command loop or
when the functions to read command input are called.
For example, the function that implements numeric prefix arguments
reads any number of digits. When it finds a non-digit event, it
must unread the event so that it can be read normally by the
command loop. Likewise, incremental search uses this feature to
unread events it does not recognize.
- Variable: unread-command-char
This variable holds a character to be read as command input. A
value of -1 means "empty".
This variable is pretty much obsolete now that you can use
`unread-command-events' instead; it exists only to support programs
written for Emacs versions 18 and earlier.
- Function: listify-key-sequence KEY
This function converts the string or vector KEY to a list of
events which you can put in `unread-command-events'. Converting a
vector is simple, but converting a string is tricky because of the
special representation used for meta characters in a string (*note
Strings of Events::.).
- Function: input-pending-p
This function determines whether any command input is currently
available to be read. It returns immediately, with value `t' if
there is input, `nil' otherwise. On rare occasions it may return
`t' when no input is available.
- Variable: last-input-event
- Variable: last-input-char
This variable records the last terminal input event read, whether
as part of a command or explicitly by a Lisp program.
In the example below, a character is read (the character `1',
ASCII code 49). It becomes the value of `last-input-char', while
`C-e' (from the `C-x C-e' command used to evaluate this
expression) remains the value of `last-command-char'.
(progn (print (read-char))
(print last-command-char)
last-input-char)
-| 49
-| 5
=> 49
The alias `last-input-char' exists for compatibility with Emacs
version 18.
- Function: discard-input
This function discards the contents of the terminal input buffer
and cancels any keyboard macro that might be in the process of
definition. It returns `nil'.
In the following example, the user may type a number of characters
right after starting the evaluation of the form. After the
`sleep-for' finishes sleeping, any characters that have been typed
are discarded.
(progn (sleep-for 2)
(discard-input))
=> nil
File: elisp, Node: Waiting, Next: Quitting, Prev: Reading Input, Up: Command Loop
Waiting for Elapsed Time or Input
=================================
The waiting commands are designed to make Emacs wait for a certain
amount of time to pass or until there is input. For example, you may
wish to pause in the middle of a computation to allow the user time to
view the display. `sit-for' pauses and updates the screen, and returns
immediately if input comes in, while `sleep-for' pauses without
updating the screen.
- Function: sit-for SECONDS &optional MILLISEC NODISP
This function performs redisplay (provided there is no pending
input from the user), then waits SECONDS seconds, or until input is
available. The result is `t' if `sit-for' waited the full time
with no input arriving (see `input-pending-p' in *Note Peeking and
Discarding::). Otherwise, the value is `nil'.
The optional argument MILLISEC specifies an additional waiting
period measured in milliseconds. This adds to the period
specified by SECONDS. Not all operating systems support waiting
periods other than multiples of a second; on those that do not,
you get an error if you specify nonzero MILLISEC.
Redisplay is always preempted if input arrives, and does not
happen at all if input is available before it starts. Thus, there
is no way to force screen updating if there is pending input;
however, if there is no input pending, you can force an update
with no delay by using `(sit-for 0)'.
If NODISP is non-`nil', then `sit-for' does not redisplay, but it
still returns as soon as input is available (or when the timeout
elapses).
The usual purpose of `sit-for' is to give the user time to read
text that you display.
- Function: sleep-for SECONDS &optional MILLISEC
This function simply pauses for SECONDS seconds without updating
the display. It pays no attention to available input. It returns
`nil'.
The optional argument MILLISEC specifies an additional waiting
period measured in milliseconds. This adds to the period
specified by SECONDS. Not all operating systems support waiting
periods other than multiples of a second; on those that do not,
you get an error if you specify nonzero MILLISEC.
Use `sleep-for' when you wish to guarantee a delay.
*Note Time of Day::, for functions to get the current time.
File: elisp, Node: Quitting, Next: Prefix Command Arguments, Prev: Waiting, Up: Command Loop
Quitting
========
Typing `C-g' while the command loop has run a Lisp function causes
Emacs to "quit" whatever it is doing. This means that control returns
to the innermost active command loop.
Typing `C-g' while the command loop is waiting for keyboard input
does not cause a quit; it acts as an ordinary input character. In the
simplest case, you cannot tell the difference, because `C-g' normally
runs the command `keyboard-quit', whose effect is to quit. However,
when `C-g' follows a prefix key, the result is an undefined key. The
effect is to cancel the prefix key as well as any prefix argument.
In the minibuffer, `C-g' has a different definition: it aborts out
of the minibuffer. This means, in effect, that it exits the minibuffer
and then quits. (Simply quitting would return to the command loop
*within* the minibuffer.) The reason why `C-g' does not quit directly
when the command reader is reading input is so that its meaning can be
redefined in the minibuffer in this way. `C-g' following a prefix key
is not redefined in the minibuffer, and it has its normal effect of
canceling the prefix key and prefix argument. This too would not be
possible if `C-g' quit directly.
`C-g' causes a quit by setting the variable `quit-flag' to a
non-`nil' value. Emacs checks this variable at appropriate times and
quits if it is not `nil'. Setting `quit-flag' non-`nil' in any way
thus causes a quit.
At the level of C code, quits cannot happen just anywhere; only at
the special places which check `quit-flag'. The reason for this is
that quitting at other places might leave an inconsistency in Emacs's
internal state. Because quitting is delayed until a safe place,
quitting cannot make Emacs crash.
Certain functions such as `read-key-sequence' or `read-quoted-char'
prevent quitting entirely even though they wait for input. Instead of
quitting, `C-g' serves as the requested input. In the case of
`read-key-sequence', this serves to bring about the special behavior of
`C-g' in the command loop. In the case of `read-quoted-char', this is
so that `C-q' can be used to quote a `C-g'.
You can prevent quitting for a portion of a Lisp function by binding
the variable `inhibit-quit' to a non-`nil' value. Then, although `C-g'
still sets `quit-flag' to `t' as usual, the usual result of this--a
quit--is prevented. Eventually, `inhibit-quit' will become `nil'
again, such as when its binding is unwound at the end of a `let' form.
At that time, if `quit-flag' is still non-`nil', the requested quit
happens immediately. This behavior is ideal for a "critical section",
where you wish to make sure that quitting does not happen within that
part of the program.
In some functions (such as `read-quoted-char'), `C-g' is handled in
a special way which does not involve quitting. This is done by reading
the input with `inhibit-quit' bound to `t' and setting `quit-flag' to
`nil' before `inhibit-quit' becomes `nil' again. This excerpt from the
definition of `read-quoted-char' shows how this is done; it also shows
that normal quitting is permitted after the first character of input.
(defun read-quoted-char (&optional prompt)
"...DOCUMENTATION..."
(let ((count 0) (code 0) char)
(while (< count 3)
(let ((inhibit-quit (zerop count))
(help-form nil))
(and prompt (message "%s-" prompt))
(setq char (read-char))
(if inhibit-quit (setq quit-flag nil)))
...)
(logand 255 code)))
- Variable: quit-flag
If this variable is non-`nil', then Emacs quits immediately,
unless `inhibit-quit' is non-`nil'. Typing `C-g' sets `quit-flag'
non-`nil', regardless of `inhibit-quit'.
- Variable: inhibit-quit
This variable determines whether Emacs should quit when `quit-flag'
is set to a value other than `nil'. If `inhibit-quit' is
non-`nil', then `quit-flag' has no special effect.
- Command: keyboard-quit
This function signals the `quit' condition with `(signal 'quit
nil)'. This is the same thing that quitting does. (See `signal'
in *Note Errors::.)
You can specify a character other than `C-g' to use for quitting.
See the function `set-input-mode' in *Note Terminal Input::.
File: elisp, Node: Prefix Command Arguments, Next: Recursive Editing, Prev: Quitting, Up: Command Loop
Prefix Command Arguments
========================
Most Emacs commands can use a "prefix argument", a number specified
before the command itself. (Don't confuse prefix arguments with prefix
keys.) The prefix argument is represented by a value that is always
available (though it may be `nil', meaning there is no prefix
argument). Each command may use the prefix argument or ignore it.
There are two representations of the prefix argument: "raw" and
"numeric". The editor command loop uses the raw representation
internally, and so do the Lisp variables that store the information, but
commands can request either representation.
Here are the possible values of a raw prefix argument:
* `nil', meaning there is no prefix argument. Its numeric value is
1, but numerous commands make a distinction between `nil' and the
integer 1.
* An integer, which stands for itself.
* A list of one element, which is an integer. This form of prefix
argument results from one or a succession of `C-u''s with no
digits. The numeric value is the integer in the list, but some
commands make a distinction between such a list and an integer
alone.
* The symbol `-'. This indicates that `M--' or `C-u -' was typed,
without following digits. The equivalent numeric value is -1, but
some commands make a distinction between the integer -1 and the
symbol `-'.
The various possibilities may be illustrated by calling the following
function with various prefixes:
(defun display-prefix (arg)
"Display the value of the raw prefix arg."
(interactive "P")
(message "%s" arg))
Here are the results of calling `print-prefix' with various raw prefix
arguments:
M-x print-prefix -| nil
C-u M-x print-prefix -| (4)
C-u C-u M-x print-prefix -| (16)
C-u 3 M-x print-prefix -| 3
M-3 M-x print-prefix -| 3 ; (Same as `C-u 3'.)
C-u - M-x print-prefix -| -
M- - M-x print-prefix -| - ; (Same as `C-u -'.)
C-u -7 M-x print-prefix -| -7
M- -7 M-x print-prefix -| -7 ; (Same as `C-u -7'.)
Emacs uses two variables to store the prefix argument: `prefix-arg'
and `current-prefix-arg'. Commands such as `universal-argument' that
set up prefix arguments for other commands store them in `prefix-arg'.
In contrast, `current-prefix-arg' conveys the prefix argument to the
current command, so setting it has no effect on the prefix arguments
for future commands.
Normally, commands specify which representation to use for the prefix
argument, either numeric or raw, in the `interactive' declaration.
(*Note Interactive Call::.) Alternatively, functions may look at the
value of the prefix argument directly in the variable
`current-prefix-arg', but this is less clean.
Do not call the functions `universal-argument', `digit-argument', or
`negative-argument' unless you intend to let the user enter the prefix
argument for the *next* command.
- Command: universal-argument
This command reads input and specifies a prefix argument for the
following command. Don't call this command yourself unless you
know what you are doing.
- Command: digit-argument ARG
This command adds to the prefix argument for the following
command. The argument ARG is the raw prefix argument as it was
before this command; it is used to compute the updated prefix
argument. Don't call this command yourself unless you know what
you are doing.
- Command: negative-argument ARG
This command adds to the numeric argument for the next command.
The argument ARG is the raw prefix argument as it was before this
command; its value is negated to form the new prefix argument.
Don't call this command yourself unless you know what you are
doing.
- Function: prefix-numeric-value ARG
This function returns the numeric meaning of a valid raw prefix
argument value, ARG. The argument may be a symbol, a number, or a
list. If it is `nil', the value 1 is returned; if it is any other
symbol, the value -1 is returned. If it is a number, that number
is returned; if it is a list, the CAR of that list (which should
be a number) is returned.
- Variable: current-prefix-arg
This variable is the value of the raw prefix argument for the
*current* command. Commands may examine it directly, but the usual
way to access it is with `(interactive "P")'.
- Variable: prefix-arg
The value of this variable is the raw prefix argument for the
*next* editing command. Commands that specify prefix arguments for
the following command work by setting this variable.
File: elisp, Node: Recursive Editing, Next: Disabling Commands, Prev: Prefix Command Arguments, Up: Command Loop
Recursive Editing
=================
The Emacs command loop is entered automatically when Emacs starts up.
This top-level invocation of the command loop is never exited until the
Emacs is killed. Lisp programs can also invoke the command loop. Since
this makes more than one activation of the command loop, we call it
"recursive editing". A recursive editing level has the effect of
suspending whatever command invoked it and permitting the user to do
arbitrary editing before resuming that command.
The commands available during recursive editing are the same ones
available in the top-level editing loop and defined in the keymaps.
Only a few special commands exit the recursive editing level; the others
return to the recursive editing level when finished. (The special
commands for exiting are always available, but do nothing when recursive
editing is not in progress.)
All command loops, including recursive ones, set up all-purpose error
handlers so that an error in a command run from the command loop will
not exit the loop.
Minibuffer input is a special kind of recursive editing. It has a
few special wrinkles, such as enabling display of the minibuffer and the
minibuffer window, but fewer than you might suppose. Certain keys
behave differently in the minibuffer, but that is only because of the
minibuffer's local map; if you switch windows, you get the usual Emacs
commands.
To invoke a recursive editing level, call the function
`recursive-edit'. This function contains the command loop; it also
contains a call to `catch' with tag `exit', which makes it possible to
exit the recursive editing level by throwing to `exit' (*note Catch and
Throw::.). If you throw a value other than `t', then `recursive-edit'
returns normally to the function that called it. The command `C-M-c'
(`exit-recursive-edit') does this. Throwing a `t' value causes
`recursive-edit' to quit, so that control returns to the command loop
one level up. This is called "aborting", and is done by `C-]'
(`abort-recursive-edit').
Most applications should not use recursive editing, except as part of
using the minibuffer. Usually it is more convenient for the user if you
change the major mode of the current buffer temporarily to a special
major mode, which has a command to go back to the previous mode. (This
technique is used by the `w' command in Rmail.) Or, if you wish to
give the user different text to edit "recursively", create and select a
new buffer in a special mode. In this mode, define a command to
complete the processing and go back to the previous buffer. (The `m'
command in Rmail does this.)
Recursive edits are useful in debugging. You can insert a call to
`debug' into a function definition as a sort of breakpoint, so that you
can look around when the function gets there. `debug' invokes a
recursive edit but also provides the other features of the debugger.
Recursive editing levels are also used when you type `C-r' in
`query-replace' or use `C-x q' (`kbd-macro-query').
- Function: recursive-edit
This function invokes the editor command loop. It is called
automatically by the initialization of Emacs, to let the user begin
editing. When called from a Lisp program, it enters a recursive
editing level.
In the following example, the function `simple-rec' first advances
point one word, then enters a recursive edit, printing out a
message in the echo area. The user can then do any editing
desired, and then type `C-M-c' to exit and continue executing
`simple-rec'.
(defun simple-rec ()
(forward-word 1)
(message "Recursive edit in progress.")
(recursive-edit)
(forward-word 1))
=> simple-rec
(simple-rec)
=> nil
- Command: exit-recursive-edit
This function exits from the innermost recursive edit (including
minibuffer input). Its definition is effectively `(throw 'exit
nil)'.
- Command: abort-recursive-edit
This function aborts the command that requested the innermost
recursive edit (including minibuffer input), by signaling `quit'
after exiting the recursive edit. Its definition is effectively
`(throw 'exit t)'. *Note Quitting::.
- Command: top-level
This function exits all recursive editing levels; it does not
return a value, as it jumps completely out of any computation
directly back to the main command loop.
- Function: recursion-depth
This function returns the current depth of recursive edits. When
no recursive edit is active, it returns 0.
File: elisp, Node: Disabling Commands, Next: Command History, Prev: Recursive Editing, Up: Command Loop
Disabling Commands
==================
"Disabling a command" marks the command as requiring user
confirmation before it can be executed. Disabling is used for commands
which might be confusing to beginning users, to prevent them from using
the commands by accident.
The low-level mechanism for disabling a command is to put a
non-`nil' `disabled' property on the Lisp symbol for the command.
These properties are normally set up by the user's `.emacs' file with
Lisp expressions such as this:
(put 'upcase-region 'disabled t)
For a few commands, these properties are present by default and may be
removed by the `.emacs' file.
If the value of the `disabled' property is a string, that string is
included in the message printed when the command is used:
(put 'delete-region 'disabled
"Text deleted this way cannot be yanked back!\n")
*Note Disabling: (emacs)Disabling, for the details on what happens
when a disabled command is invoked interactively. Disabling a command
has no effect on calling it as a function from Lisp programs.
- Command: enable-command COMMAND
Allow COMMAND to be executed without special confirmation from now
on. The user's `.emacs' file is optionally altered so that this
will apply to future sessions.
- Command: disable-command COMMAND
Require special confirmation to execute COMMAND from now on. The
user's `.emacs' file is optionally altered so that this will apply
to future sessions.
- Variable: disabled-command-hook
This variable is a normal hook that is run instead of a disabled
command, when the user runs the disabled command interactively.
The hook functions can use `this-command-keys' to determine what
the user typed to run the command, and thus find the command
itself.
By default, `disabled-command-hook' contains a function that asks
the user whether to proceed.
File: elisp, Node: Command History, Next: Keyboard Macros, Prev: Disabling Commands, Up: Command Loop
Command History
===============
The command loop keeps a history of the complex commands that have
been executed, to make it convenient to repeat these commands. A
"complex command" is one for which the interactive argument reading
uses the minibuffer. This includes any `M-x' command, any `M-ESC'
command, and any command whose `interactive' specification reads an
argument from the minibuffer. Explicit use of the minibuffer during
the execution of the command itself does not cause the command to be
considered complex.
- Variable: command-history
This variable's value is a list of recent complex commands, each
represented as a form to evaluate. It continues to accumulate all
complex commands for the duration of the editing session, but all
but the first (most recent) thirty elements are deleted when a
garbage collection takes place (*note Garbage Collection::.).
command-history
=> ((switch-to-buffer "chistory.texi")
(describe-key "^X^[")
(visit-tags-table "~/emacs/src/")
(find-tag "repeat-complex-command"))
This history list is actually a special case of minibuffer history
(*note Minibuffer History::.), with one special twist: the elements are
expressions rather than strings.
There are a number of commands devoted to the editing and recall of
previous commands. The commands `repeat-complex-command', and
`list-command-history' are described in the user manual (*note
Repetition: (emacs)Repetition.). Within the minibuffer, the history
commands used are the same ones available in any minibuffer.
File: elisp, Node: Keyboard Macros, Prev: Command History, Up: Command Loop
Keyboard Macros
===============
A "keyboard macro" is a canned sequence of input events that can be
considered a command and made the definition of a key. Don't confuse
keyboard macros with Lisp macros (*note Macros::.).
- Function: execute-kbd-macro MACRO &optional COUNT
This function executes MACRO as a sequence of events. If MACRO is
a string or vector, then the events in it are executed exactly as
if they had been input by the user. The sequence is *not*
expected to be a single key sequence; normally a keyboard macro
definition consists of several key sequences concatenated.
If MACRO is a symbol, then its function definition is used in
place of MACRO. If that is another symbol, this process repeats.
Eventually the result should be a string or vector. If the result
is not a symbol, string, or vector, an error is signaled.
The argument COUNT is a repeat count; MACRO is executed that many
times. If COUNT is omitted or `nil', MACRO is executed once. If
it is 0, MACRO is executed over and over until it encounters an
error or a failing search.
- Variable: last-kbd-macro
This variable is the definition of the most recently defined
keyboard macro. Its value is a string or vector, or `nil'.
- Variable: executing-macro
This variable contains the string or vector that defines the
keyboard macro that is currently executing. It is `nil' if no
macro is currently executing.
- Variable: defining-kbd-macro
This variable indicates whether a keyboard macro is being defined.
It is set to `t' by `start-kbd-macro', and `nil' by
`end-kbd-macro'. You can use this variable to make a command
behave differently when run from a keyboard macro (perhaps
indirectly by calling `interactive-p'). However, do not set this
variable yourself.
The commands are described in the user's manual (*note Keyboard
Macros: (emacs)Keyboard Macros.).
File: elisp, Node: Keymaps, Next: Modes, Prev: Command Loop, Up: Top
Keymaps
*******
The bindings between input events and commands are recorded in data
structures called "keymaps". Each binding in a keymap associates (or
"binds") an individual event type either with another keymap or with a
command. When an event is bound to a keymap, that keymap is used to
look up the next character typed; this continues until a command is
found. The whole process is called "key lookup".
* Menu:
* Keymap Terminology:: Definitions of terms pertaining to keymaps.
* Format of Keymaps:: What a keymap looks like as a Lisp object.
* Creating Keymaps:: Functions to create and copy keymaps.
* Inheritance and Keymaps:: How one keymap can inherit the bindings
of another keymap.
* Prefix Keys:: Defining a key with a keymap as its definition.
* Menu Keymaps:: A keymap can define a menu.
* Active Keymaps:: Each buffer has a local keymap
to override the standard (global) bindings.
A minor mode can also override them.
* Key Lookup:: How extracting elements from keymaps works.
* Functions for Key Lookup:: How to request key lookup.
* Changing Key Bindings:: Redefining a key in a keymap.
* Key Binding Commands:: Interactive interfaces for redefining keys.
* Scanning Keymaps:: Looking through all keymaps, for printing help.
File: elisp, Node: Keymap Terminology, Next: Format of Keymaps, Up: Keymaps
Keymap Terminology
==================
A "keymap" is a table mapping event types to definitions (which can
be any Lisp objects, though only certain types are meaningful for
execution by the command loop). Given an event (or an event type) and a
keymap, Emacs can get the event's definition. Events include ordinary
ASCII characters, function keys, and mouse actions (*note Input
Events::.).
A sequence of input events that form a unit is called a "key
sequence", or "key" for short. A sequence of one event is always a key
sequence, and so are some multi-event sequences.
A keymap determines a binding or definition for any key sequence. If
the key sequence is a single event, its binding is the definition of the
event in the keymap. The binding of a key sequence of more than one
event is found by an iterative process: the binding of the first event
is found, and must be a keymap; then the second event's binding is found
in that keymap, and so on until all the events in the key sequence are
used up.
If the binding of a key sequence is a keymap, we call the key
sequence a "prefix key". Otherwise, we call it a "complete key"
(because no more characters can be added to it). If the binding is
`nil', we call the key "undefined". Examples of prefix keys are `C-c',
`C-x', and `C-x 4'. Examples of defined complete keys are `X', RET,
and `C-x 4 C-f'. Examples of undefined complete keys are `C-x C-g',
and `C-c 3'. *Note Prefix Keys::, for more details.
The rule for finding the binding of a key sequence assumes that the
intermediate bindings (found for the events before the last) are all
keymaps; if this is not so, the sequence of events does not form a
unit--it is not really a key sequence. In other words, removing one or
more events from the end of any valid key must always yield a prefix
key. For example, `C-f C-f' is not a key; `C-f' is not a prefix key,
so a longer sequence starting with `C-f' cannot be a key.
Note that the set of possible multi-event key sequences depends on
the bindings for prefix keys; therefore, it can be different for
different keymaps, and can change when bindings are changed. However,
a one-event sequence is always a key sequence, because it does not
depend on any prefix keys for its well-formedness.
At any time, several primary keymaps are "active"--that is, in use
for finding key bindings. These are the "global map", which is shared
by all buffers; the "local keymap", which is usually associated with a
specific major mode; and zero or more "minor mode keymaps" which belong
to currently enabled minor modes. (Not all minor modes have keymaps.)
The local keymap bindings shadow (i.e., take precedence over) the
corresponding global bindings. The minor mode keymaps shadow both
local and global keymaps. *Note Active Keymaps::, for details.
File: elisp, Node: Format of Keymaps, Next: Creating Keymaps, Prev: Keymap Terminology, Up: Keymaps
Format of Keymaps
=================
A keymap is a list whose CAR is the symbol `keymap'. The remaining
elements of the list define the key bindings of the keymap. Use the
function `keymapp' (see below) to test whether an object is a keymap.
An ordinary element is a cons cell of the form `(TYPE . BINDING)'.
This specifies one binding which applies to events of type TYPE. Each
ordinary binding applies to events of a particular "event type", which
is always a character or a symbol. *Note Classifying Events::.
A cons cell whose CAR is `t' is a "default key binding"; any event
not bound by other elements of the keymap is given BINDING as its
binding. Default bindings allow a keymap to bind all possible event
types without having to enumerate all of them. A keymap that has a
default binding completely masks any lower-precedence keymap.
If an element of a keymap is a vector, the vector counts as bindings
for all the ASCII characters; vector element N is the binding for the
character with code N. This is a more compact way to record lots of
bindings. A keymap with such a vector is called a "full keymap".
Other keymaps are called "sparse keymaps".
When a keymap contains a vector, it always defines a binding for
every ASCII character even if the vector element is `nil'. Such a
binding of `nil' overrides any default binding in the keymap. However,
default bindings are still meaningful for events that are not ASCII
characters. A binding of `nil' does *not* override lower-precedence
keymaps; thus, if the local map gives a binding of `nil', Emacs uses
the binding from the global map.
Aside from bindings, a keymap can also have a string as an element.
This is called the "overall prompt string" and makes it possible to use
the keymap as a menu. *Note Menu Keymaps::.
Keymaps do not directly record bindings for the meta characters,
whose codes are from 128 to 255. Instead, meta characters are regarded
for purposes of key lookup as sequences of two characters, the first of
which is ESC (or whatever is currently the value of
`meta-prefix-char'). Thus, the key `M-a' is really represented as `ESC
a', and its global binding is found at the slot for `a' in `esc-map'.
Here as an example is the local keymap for Lisp mode, a sparse
keymap. It defines bindings for DEL and TAB, plus `C-c C-l', `M-C-q',
and `M-C-x'.
lisp-mode-map
=>
(keymap
;; TAB
(9 . lisp-indent-line)
;; DEL
(127 . backward-delete-char-untabify)
(3 keymap
;; `C-c C-l'
(12 . run-lisp))
(27 keymap
;; `M-C-q', treated as `ESC C-q'
(17 . indent-sexp)
;; `M-C-x', treated as `ESC C-x'
(24 . lisp-send-defun)))
- Function: keymapp OBJECT
This function returns `t' if OBJECT is a keymap, `nil' otherwise.
Practically speaking, this function tests for a list whose CAR is
`keymap'.
(keymapp '(keymap))
=> t
(keymapp (current-global-map))
=> t
File: elisp, Node: Creating Keymaps, Next: Inheritance and Keymaps, Prev: Format of Keymaps, Up: Keymaps
Creating Keymaps
================
Here we describe the functions for creating keymaps.
- Function: make-keymap &optional PROMPT
This function creates and returns a new full keymap (i.e., one
which contains a vector of length 128 for defining all the ASCII
characters). The new keymap initially binds all ASCII characters
to `nil', and does not bind any other kind of event.
(make-keymap)
=> (keymap [nil nil nil ... nil nil])
If you specify PROMPT, that becomes the overall prompt string for
the keymap. The prompt string is useful for menu keymaps (*note
Menu Keymaps::.).
- Function: make-sparse-keymap &optional PROMPT
This function creates and returns a new sparse keymap with no
entries. The new keymap does not bind any events. The argument
PROMPT specifies a prompt string, as in `make-keymap'.
(make-sparse-keymap)
=> (keymap)
- Function: copy-keymap KEYMAP
This function returns a copy of KEYMAP. Any keymaps which appear
directly as bindings in KEYMAP are also copied recursively, and so
on to any number of levels. However, recursive copying does not
take place when the definition of a character is a symbol whose
function definition is a keymap; the same symbol appears in the
new copy.
(setq map (copy-keymap (current-local-map)))
=> (keymap
;; (This implements meta characters.)
(27 keymap
(83 . center-paragraph)
(115 . center-line))
(9 . tab-to-tab-stop))
(eq map (current-local-map))
=> nil
(equal map (current-local-map))
=> t